<template>
  <el-row>
    <el-col :span="24">
      <el-card class="search-form">
        <el-form ref="searchForm" class="el-form-col el-form-s" :model="search">
          <el-row :gutter="15">
            <el-col :span="6">
              <el-form-item prop="phone" label="">
                <el-input v-model="search.phone" placeholder="绑定登录手机号" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="" prop="name">
                <el-input v-model="search.name" placeholder="姓名"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="" prop="position">
                <el-input v-model="search.position" placeholder="岗位"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="2" class="text-center">
              <el-button type="primary" :loading="searchLoading" plain native-type="submit" @click.prevent="searchFunc(search)"><span>搜索</span></el-button>
            </el-col>
            <el-col :span="2" class="text-center">
              <el-button type="primary" plain @click="isOpen = !isOpen">{{ isOpen ? '收起' : '展开' }}</el-button>
            </el-col>
            <el-col :span="2" class="text-center">
              <el-button @click="resetFields" plain>重置</el-button>
            </el-col>
          </el-row>

          <el-collapse-transition>
            <template  v-if="isOpen">
              <el-row :gutter="15">
                <el-col :span="6">
                  <el-form-item label="" prop="state">
                    <el-select v-model="search.state" clearable placeholder="状态">
                      <el-option label="启用" value="1"></el-option>
                      <el-option label="停用" value="0"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="" prop="roleId">
                    <el-select v-model="search.roleId" clearable placeholder="角色名称">
                      <el-option :key="item.value" v-for="item in roleArr" :label="item.text" :value="item.value">
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="6" v-if="routeName === 'infoUserIndex'">
                  <el-form-item label="" prop="company">
                    <el-input v-model="search.company" placeholder="所属公司编号"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="6" v-if="routeName === 'infoUserIndex'">
                  <el-form-item label="" prop="companyCode">
                    <el-select v-model="search.companyCode" clearable placeholder="所属公司名称">
                      <el-option  v-for="item in companyArr" :key="item.value" :label="item.text" :value="item.value">
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="6" v-if="routeName === 'infoUserIndex'">
                  <el-form-item label="" prop="founderUserName">
                    <el-input v-model="search.founderUserName" placeholder="创建人"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </template>
          </el-collapse-transition>
        </el-form>
      </el-card>
    </el-col>
    <el-col :span="24">
      <BasicTable
        ref="BasicTable"
        url="/ms-warehouse-order/user/findStorageUserList"
        :tabs="tabs"
        :initParams="(this.routeName === 'infoUserIndex') ? allParams : myCompanyParams"
        :columns="columns">
        <template slot="buttons">
          <el-button type="primary" @click="show(false)">新增</el-button>
        </template>
        <template slot="column">
          <el-table-column fixed="right" width="100px" align="center" label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="stopAndStart(scope.row, 'start')"
                v-if="scope.row.state === '0'"
              >
                启用
              </el-button>
              <el-button
                size="mini"
                type="text"
                @click="stopAndStart(scope.row, 'stop')"
                v-if="scope.row.state === '1'"
              >
                停用
              </el-button>
              <el-button
                size="mini"
                type="text"
                @click="show(scope.row)"
              >
                详情
              </el-button>
            </template>
          </el-table-column>
        </template>
      </BasicTable>
    </el-col>
  </el-row>
</template>
<script>
import {copy, dotData, confirm} from '@/utils'
import {getCompanyName} from '@/utils/selectOptions'
import {unixTimestampFilter} from '@/filters'
export default {
  name: 'userIndex',
  components: {
  },
  data() {
    let columns = [
      {
        label: '绑定登陆手机号',
        data: 'phone'
      },
      {
        label: '姓名',
        data: 'name'
      },
      {
        label: '角色名称',
        data: 'roleName'
      },
      {
        label: '岗位',
        data: 'position'
      },
      {
        label: '所属公司编号',
        data: 'companyCode'
      },
      {
        label: '所属公司名称',
        data: 'companyName'
      },
      {
        label: '创建人',
        data: 'founderUserName'
      },
      {
        label: '创建时间',
        data: 'foundDate',
        width: '134px',
        render(data, full) {
          return unixTimestampFilter(data)
        }
      },
      {
        label: '状态',
        data: 'state',
        render(data, full) {
          switch (data) {
            case 1: return '启用'
            case 0: return '停用'
            case '1': return '启用'
            case '0': return '停用'
            default: return '未知'
          }
        }
      }
    ]
    return {
      routeName: this.$route.name,
      allParams: {
        queryStatus: 1
      },
      myCompanyParams: {
        queryStatus: 2
      },
      positionArr: [],
      companyArr: [],
      roleArr: [],
      isOpen: false,
      visible: false,
      searchLoading: false,
      search: {
        phone: '',
        name: '',
        position: '',
        state: '',
        roleId: '',
        companyId: '',
        companyCode: ''
      },
      columns,
      tabs: []
    }
  },
  created() {
    getCompanyName().then(arr => {
      this.companyArr = arr
    })
    this.getRoleArr()
    // getOrgRoleArr().then(arr => {
    //   this.roleArr = arr
    // })
  },
  methods: {
    getRoleArr() {
      this.$ajax.get('/ms-common-user/role/role-list', {projectType: 'storeWeb'}).then(response => {
        this.roleArr = response.data.map(v => {
          return {value: v.id, text: v.name}
        })
        console.log('this.roleArr', this.roleArr)
      })
    },
    // 搜索
    searchFunc(search) {
      const basicTable = this.$refs.BasicTable
      if (basicTable) {
        let params = copy(search)
        params.provinceNo = dotData(params, 'addressArr[0]') || ''
        params.cityNo = dotData(params, 'addressArr[1]') || ''
        params.districtNo = dotData(params, 'addressArr[2]') || ''
        delete params.addressArr
        this.searchLoading = true
        basicTable.search(params).finally(_ => {
          this.searchLoading = false
        })
      }
    },
    // 重置表单
    resetFields() {
      const searchForm = this.$refs.searchForm
      if (searchForm) {
        searchForm.resetFields()
      }
    },
    // 表格刷新
    refresh() {
      this.searchFunc(this.search)
    },
    show(data) {
      if (data === false) { // 新增
        if (this.routeName === 'userIndex') {
          this.$router.push({path: '/ss-oms/user/create'})
        } else {
          this.$router.push({path: '/ss-oms/info/user/create'})
        }
      } else {
        if (this.routeName === 'userIndex') { // 进详情
          this.$router.push({path: '/ss-oms/user/show/' + data.userId})
        } else {
          this.$router.push({path: '/ss-oms/info/user/show/' + data.userId})
        }
      }
    },
    stopAndStart(data, operation) {
      let title = ''
      let state = ''
      let msg = ''
      if (operation === 'stop') {
        title = '停用'
        state = '0'
        msg = '<p>确认要停用该账号吗？</p><p>停用后账号将不能再登录系统</p>'
      } else {
        title = '启用'
        state = '1'
        msg = '<p>确定要启用该账号吗？</p><p>启用后账号对应的权限将开放使用。</p>'
      }
      confirm(msg, title, {
        confirmButtonLoading: true,
        type: 'warning',
        dangerouslyUseHTMLString: true,
        confirmCallBack: ({confirmButtonLoadingClose, close}) => {
          return this.$ajax.post('/ms-warehouse-order/user/isEnable', {
            userId: data.userId,
            state: state
          }).then(response => {
            if (response.success) {
              close().then(_ => {
                this.$message({
                  type: 'success',
                  message: '操作成功!'
                })
                this.refresh()
              })
            } else {
              confirmButtonLoadingClose()
            }
          }).finally(_ => {
            confirmButtonLoadingClose()
          })
        }
      })
    }
  }
}
</script>
